﻿Imports DevComponents.DotNetBar


Public Class ucDSNhasanxuat

    Private dtNSX As DataTable
    Dim nsxBus As New ManufactureBUS()
    'Public eActive As in

    Private Sub newButton_Click(sender As Object, e As EventArgs) Handles newButton.Click
        'eActive = mdQuanLyBanHang.ActionType.AddRecord
        Dim uc As New ucCapnhatnhasx(0, grdItem.SelectedRows(0))
        Me.Controls.Add(uc)
        uc.Dock = DockStyle.Fill
        AddHandler uc.Closed, AddressOf uc_Closed

        Dim parent = DirectCast(Application.OpenForms.Item(0), DevComponents.DotNetBar.Metro.MetroAppForm)
        parent.ShowModalPanel(uc, DevComponents.DotNetBar.Controls.eSlideSide.Left)
    End Sub

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        mdQuanLyBanHang.InitQuanLyBanHang()
        LoadData()
        ReloadWidthColumn()
        LoadComboboxSearch()
    End Sub

    Public Sub LoadData()
        dtNSX = nsxBus.Manufacture_List().Tables(0)
        grdItem.DataSource = dtNSX
        grdItem.Sort(grdItem.Columns(1), System.ComponentModel.ListSortDirection.Ascending)
    End Sub

    Public Sub LoadComboboxSearch()
        For Each col As DataGridViewColumn In grdItem.Columns
            If col.Visible = True Then
                Dim ci As New ComboBoxItem(col.Name, col.HeaderText)
                ci.Tag = col.Tag
                cbSearchIn.Items.Add(ci)
            End If
        Next

        cbSearchIn.SelectedIndex = 0
    End Sub

    Public Sub ReloadWidthColumn()
        grdItem.Columns(0).Width = 70
        grdItem.Columns(0).Visible = False
        grdItem.Sort(grdItem.Columns(1), System.ComponentModel.ListSortDirection.Ascending)

        grdItem.Columns(1).Width = 150
        grdItem.Columns(1).HeaderText = "Tên nhà sản xuất"
        grdItem.Columns(1).Name = "tennhasanxuat"
        grdItem.Columns(1).Tag = "String"

        grdItem.Columns(2).Width = 170
        grdItem.Columns(2).HeaderText = "Mô tả"
        grdItem.Columns(2).Name = "mota"
        grdItem.Columns(2).Tag = "String"

        grdItem.Columns(3).Width = 150
        grdItem.Columns(3).HeaderText = "Ngày tạo"
        grdItem.Columns(3).Name = "ngaytao"
        grdItem.Columns(3).Tag = "Date"

        grdItem.Columns(4).Width = 150
        grdItem.Columns(4).HeaderText = "Người tạo"
        grdItem.Columns(4).Name = "nguoitao"
        grdItem.Columns(4).Tag = "String"

        grdItem.Columns(5).Width = 150
        grdItem.Columns(5).HeaderText = "Ngày sửa"
        grdItem.Columns(5).Name = "ngaysua"
        grdItem.Columns(5).Tag = "Date"

        grdItem.Columns(6).Width = 150
        grdItem.Columns(6).HeaderText = "Người sửa"
        grdItem.Columns(6).Name = "nguoisua"
        grdItem.Columns(6).Tag = "String"

    End Sub

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub

    Private Sub grdItem_ColumnSortModeChanged(sender As Object, e As DataGridViewColumnEventArgs) Handles grdItem.ColumnSortModeChanged

    End Sub

    Private Sub FindNow_Click(sender As Object, e As EventArgs) Handles FindNow.Click
        If validation() = False Then
            Exit Sub
        Else
            grdItem.DataSource = mdQuanLyBanHang.SearchbyComboboxValue(grdItem, _
                                                                   cbSearchIn.SelectedItem.ToString(), _
                                                                   cbSearchIn.Items(cbSearchIn.SelectedIndex), _
                                                                   tbSearchStr.Text, _
                                                                   dtNSX)
            grdItem.Sort(grdItem.Columns(1), System.ComponentModel.ListSortDirection.Ascending)
        End If
    End Sub

    Private Sub tbSearchStr_KeyDown(sender As Object, e As KeyEventArgs) Handles tbSearchStr.KeyDown
        If e.KeyData = Keys.Enter Then
            If Not validation() Then
                Exit Sub
            End If
            grdItem.DataSource = mdQuanLyBanHang.SearchbyComboboxValue(grdItem, _
                                                                       cbSearchIn.SelectedItem.ToString(), _
                                                                       cbSearchIn.SelectedItem.ToString(), _
                                                                       tbSearchStr.Text, _
                                                                       dtNSX)
            grdItem.Sort(grdItem.Columns(1), System.ComponentModel.ListSortDirection.Ascending)
        End If
    End Sub

    Private Function validation() As Boolean
        Try
            BalloonTip1.SetBalloonCaption(DateTimeInputDenngay, "Lỗi tìm kiếm")
            Select Case cbSearchIn.SelectedIndex
                Case 2, 4
                    'Dim arrstr As String()
                    'arrstr = tbSearchStr.Text.Split("-")
                    'If Trim(arrstr(0)) = "" Or Trim(arrstr(1)) = "" Or Trim(arrstr(2)) = "" Then
                    '    BalloonTip1.SetBalloonText(tbSearchStr, "Vui lòng nhập ngày tháng")
                    '    BalloonTip1.ShowBalloon(tbSearchStr)
                    '    Return False
                    'End If

                    'If Integer.Parse(arrstr(0)) > 31 Or Integer.Parse(arrstr(0)) < 0 Then
                    '    Return False
                    'End If

                    'If Integer.Parse(arrstr(1)) < 0 Or Integer.Parse(arrstr(1)) > 12 Then
                    '    Return False
                    'End If

                    If DateTimeInputTungay.Value > DateTimeInputDenngay.Value Then
                        BalloonTip1.SetBalloonText(DateTimeInputDenngay, "Ngày bắt đầu phải trước ngày kết thúc")
                        BalloonTip1.ShowBalloon(DateTimeInputDenngay)
                        Return False
                    End If


                Case Else
                    If tbSearchStr.Text = "" Then
                        BalloonTip1.SetBalloonText(tbSearchStr, "Vui lòng nhập thông tin")
                        BalloonTip1.ShowBalloon(tbSearchStr)
                        Return False
                    End If
            End Select
            Return True

        Catch ex As Exception

        End Try

        Return True
    End Function

    Public Sub showdatecontrols()
        tbSearchStr.Visible = False
        LabelXTukhoa.Visible = False
        LabelXTungay.Visible = True
        DateTimeInputTungay.Visible = True
        LabelXDenngay.Visible = True
        DateTimeInputDenngay.Visible = True
    End Sub

    Public Sub hidedatecontrols()
        tbSearchStr.Visible = True
        LabelXTukhoa.Visible = True
        LabelXTungay.Visible = False
        DateTimeInputTungay.Visible = False
        LabelXDenngay.Visible = False
        DateTimeInputDenngay.Visible = False
    End Sub

    Private Sub cbSearchIn_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbSearchIn.SelectedIndexChanged
        'If cbSearchIn.SelectedIndex = 4 Or cbSearchIn.SelectedIndex = 2 Then
        '    tbSearchStr.WatermarkText = "ngày-tháng-năm"
        'Else
        '    tbSearchStr.WatermarkText = ""
        'End If
        tbSearchStr.AsciiOnly = True

        Select Case cbSearchIn.SelectedIndex
            Case 0
                hidedatecontrols()
                tbSearchStr.WatermarkText = "Nhập tên nhà sản xuất"
                tbSearchStr.Mask = ""
            Case 1
                hidedatecontrols()
                tbSearchStr.WatermarkText = "Nhập mô tả"
                tbSearchStr.Mask = ""
            Case 2, 4
                'tbSearchStr.WatermarkText = "Nhập theo ngày-tháng-năm"
                'tbSearchStr.Mask = "00/00/0000"
                showdatecontrols()
                DateTimeInputTungay.Value = Now
                DateTimeInputDenngay.Value = Now
            Case 3
                hidedatecontrols()
                tbSearchStr.WatermarkText = "Nhập tên người tạo"
                tbSearchStr.Mask = ""
            Case 5
                hidedatecontrols()
                tbSearchStr.WatermarkText = "Nhập tên người sửa"
                tbSearchStr.Mask = ""
            Case Else

        End Select
        tbSearchStr.Text = ""
        'tbSearchStr.ReleaseFocus()

    End Sub

    Private Sub btEdit_Click(sender As Object, e As EventArgs) Handles btEdit.Click
        Dim uc As New ucCapnhatnhasx(1, grdItem.SelectedRows(0))
        Me.Controls.Add(uc)
        uc.Dock = DockStyle.Fill
        AddHandler uc.Closed, AddressOf uc_Closed
        Dim parent = DirectCast(Application.OpenForms.Item(0), DevComponents.DotNetBar.Metro.MetroAppForm)
        parent.ShowModalPanel(uc, DevComponents.DotNetBar.Controls.eSlideSide.Left)
        If uc.ButtonItemSave.IsMouseDown Then
            LoadData()
        End If

    End Sub



    Private Sub BalloonTip1_BalloonClosing(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles BalloonTip1.BalloonClosing

    End Sub

    Private Sub BalloonTip1_BalloonDisplaying(sender As Object, e As EventArgs) Handles BalloonTip1.BalloonDisplaying

    End Sub


    Private Sub uc_Closed(sender As Object)
        'Dim uc = DirectCast(sender, ucCapnhatnhasx)
        'MessageBoxEx.Show(uc.Cancelled)
        LoadData()

    End Sub


    Private Sub deleteButton_Click(sender As Object, e As EventArgs) Handles deleteButton.Click
        Try
            If MessageBox.Show("Bạn có chắc chắn muốn xóa Nhà sản xuất " + grdItem.SelectedRows(0).Cells(1).Value.ToString(), "Thông báo", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                If nsxBus.Manufacturers_Delete(grdItem.SelectedRows(0).Cells(0).Value) Then
                    BalloonTip1.SetBalloonCaption(grdItem, "Xóa nhà sản xuất ")
                    BalloonTip1.SetBalloonText(grdItem, "Thành công")
                    BalloonTip1.ShowBalloon(grdItem)
                    LoadData()
                Else
                    BalloonTip1.SetBalloonCaption(grdItem, "Xóa nhà sản xuất ")
                    BalloonTip1.SetBalloonText(grdItem, "Thất bại ! Nhà sản xuất " + grdItem.SelectedRows(0).Cells(1).Value.ToString() + " vẫn còn hàng hóa ")
                    BalloonTip1.ShowBalloon(grdItem)
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btReport_Click(sender As Object, e As EventArgs) Handles btReport.Click
        MessageBox.Show("Đang xử lý hiển thị danh sách nhà sản xuất này, vui lòng chờ", "Thông báo", MessageBoxButtons.YesNo)
        PrintReport(False)
    End Sub

    Private Sub PrintReport(ByVal bolPrint As Boolean)
        'CircularProgress1.Visible = True

        Dim frmPrint As New frmReportViewer
        Dim strSelectionFormula As String = Nothing
        Dim rowGrid As DataGridViewRow
        'CircularProgress1.Value = 20
        Try
            For Each rowGrid In grdItem.Rows
                If Not IsNothing(strSelectionFormula) Then
                    strSelectionFormula += " OR "
                End If
                strSelectionFormula += " {TDT_Manufacturers.ManufacturerID} = " & rowGrid.Cells("ManufacturerID").Value
            Next



            frmPrint.Text = Me.Text
            frmPrint.SelectionFormula = strSelectionFormula
            If bolPrint = False Then
                frmPrint.InitializeLifetimeService()

                'CircularProgress1.Value = 80

                frmPrint.ConfigReport("crpManufacturers.rpt", False)
                'CircularProgress1.Value = 90

                frmPrint.Show()
                'CircularProgress1.Value = 100
                'CircularProgress1.Visible = False
            Else
                frmPrint.ConfigReport("crpManufacturers.rpt", True)
                'CircularProgress1.Value = 70

                frmPrint.PrintReport()
                'CircularProgress1.Value = 100
                'CircularProgress1.Visible = False
            End If

        Catch ex As Exception
            objErrorLog.SaveError(ex.Message, Me.Name, "PrintReport", objUser.UserFullName)
            'SendError(ex)
        End Try
        'Dim frmPrint As New frmReportViewer
        'Dim strSelectionFormula As String = Nothing
        'Dim rowGrid As DataGridViewRow
        'Try
        '    For Each rowGrid In grdItem.Rows
        '        If Not IsNothing(strSelectionFormula) Then
        '            strSelectionFormula += " OR "
        '        End If
        '        strSelectionFormula += " {TDT_Manufacturers.ManufacturerID} = " & rowGrid.Cells("ManufacturerID").Value
        '    Next
        '    frmPrint.Text = Me.Text
        '    frmPrint.SelectionFormula = strSelectionFormula
        '    If bolPrint = False Then
        '        frmPrint.InitializeLifetimeService()
        '        frmPrint.ConfigReport("crpManufacturers.rpt", False)
        '        frmPrint.Show()
        '    Else
        '        frmPrint.ConfigReport("crpManufacturers.rpt", True)
        '        frmPrint.PrintReport()
        '    End If
        'Catch ex As ExecutionEngineException
        '    objErrorLog.SaveError(ex.Message, Me.Name, "PrintReport", objUser.UserFullName)
        '    'SendError(ex)
        'End Try
    End Sub

    Private Sub btPrint_Click(sender As Object, e As EventArgs) Handles btPrint.Click
        If MessageBox.Show("Bạn sẽ in danh sách nhà sản xuất này", "Thông báo", MessageBoxButtons.YesNo) = DialogResult.Yes Then
            PrintReport(True)
        End If
    End Sub
End Class